﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;
using CNotebook.Sales;
namespace NotebookS2
{
    public partial class M_ADDRESSBOOK : RibbonForm
    {
        public M_ADDRESSBOOK()
        {
            InitializeComponent();
        }

        public string DocumentString { get { return "Business Partner"; } }

        public string ConnectionString { get; set; }
        
        #region Binding Records

        void BindVAT()
        {
            TaxManager oManager = new TaxManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            DataTable dt = oManager.GetTaxCodes();

            cboVATCode.DisplayMember = "Dscription";
            cboVATCode.ValueMember = "Dscription";
            
            cboVATCode.DataSource = dt;
            cboVATCode.Refresh();

            oManager.Close();
        }

        void BindPriceList()
        {
            PriceManager oManager = new PriceManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable _Table = oManager.GetPriceList();
            oManager.Close();

            cboPriceList.DataSource = _Table;
            cboPriceList.DisplayMember = "PriceListName";
            cboPriceList.ValueMember = "PriceListcode";
            cboPriceList.Refresh();

            cboPriceList.SelectedIndex = -1;


            oManager.Close();
        }

        void BindTeritorry()
        {
            TeritorryManager oManager = new TeritorryManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            cboTeritorrialCode.ValueMember = "TERITORRYCODE";
            cboTeritorrialCode.DisplayMember = "TERITORRYNAME";
            cboTeritorrialCode.DataSource = oManager.GetTeritorry();
            cboTeritorrialCode.Refresh();
            cboTeritorrialCode.SelectedIndex = -1;
            oManager.Close();
        }

        void BindPartnerGroup(bool IsCustomer)
        {
            AddressBookManager oManager = new AddressBookManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            cboGroup.ValueMember = "GROUPCODE";
            cboGroup.DisplayMember = "GROUPNAME";
            cboGroup.DataSource = oManager.GetGroupAddressBook(IsCustomer);
            cboGroup.Refresh();
            cboGroup.SelectedIndex = -1;
            oManager.Close();
        }

        void BindAccounts(string inFoType)
        {
            AccountManager oAccounts = new AccountManager();

            oAccounts.ConnectionString = this.ConnectionString;
            oAccounts.Open();
            cboSalesMan.DisplayMember = "AccountHolder";
            cboSalesMan.ValueMember = "AcctCode";

            cboSalesMan.DataSource = oAccounts.GetAccounts(inFoType);
            cboSalesMan.Refresh();
            cboSalesMan.SelectedIndex = -1;

            oAccounts.Close();
        }

        void BindConsolidating(string inFoType)
        {
            CustomerInfoManager oBusinessPartner = new CustomerInfoManager();

            oBusinessPartner.ConnectionString = this.ConnectionString;
            oBusinessPartner.Open();




            lookUpBP.Properties.DataSource = oBusinessPartner.GetAllPartnersByBPType(inFoType);
            lookUpBP.Properties.DisplayMember = "CardName";
            lookUpBP.Properties.ValueMember = "CardCode";
            //DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            //col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("CardName", "CardName", 100);

            //col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            //lookUpBP.Properties.Columns.Add(col);
            lookUpBP.Properties.ShowHeader = false;
            
            oBusinessPartner.Close();
        }

        void BindAreaCode()
        {
            AddressBookManager oManager = new AddressBookManager();
            DataTable dtAreaTable = new DataTable();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dtAreaTable = oManager.GetAreaCodes();

            cboAreaCode.ValueMember = "AreaCode";
            cboAreaCode.DisplayMember = "AreaName";

            cboAreaCode.DataSource = dtAreaTable;

            cboAreaCode.SelectedIndex = -1;
            oManager.Close();
        }

        public void OpenCustomer(string BPCode, string BPType)
        {
            AddressBookManager bp = new AddressBookManager();

            bp.ConnectionString = this.ConnectionString;
            bp.Open();
            DataTable table = bp.GetByBPCode(BPCode, BPType);
            bp.Close();
            DataRow oRow = table.Rows[0];
            if (BPCode != "")
            {
                txtCode.Text = oRow["BPCode"].ToString();
                txtName.Text = oRow["BPName"].ToString();
                //txtAddress.Text = oSearchBP.Address;

                txtUnitNo.Text = oRow["UnitNo"].ToString();
                txtStreetAddress.Text = oRow["Street"].ToString();
                txtBarangayOrDistrict.Text = oRow["Brgy"].ToString();
                txtCityOrMunicipality.Text = oRow["CityOrMunicipal"].ToString();
                txtPostalCode.Text = oRow["PostalCode"].ToString();
                txtProvinceOrMetro.Text = oRow["ProvinceOrMetro"].ToString();

                txtContactPerson.Text = oRow["ContactPerson"].ToString();
                txtFax.Text = oRow["Fax"].ToString();
                txtPhone.Text = oRow["Phone"].ToString();
                txtTIN.Text = oRow["TIN"].ToString(); ;
                txtPaymentTerm.Text = oRow["PaymentTerm"].ToString();
                txtCreditLimit.Text = oRow["CreditLimit"].ToString();
                if (oRow["PriceList"] != DBNull.Value)
                {
                    cboPriceList.SelectedValue = oRow["PriceList"].ToString();
                }
                else
                {
                    cboPriceList.SelectedIndex = -1;
                }
                if (oRow["SMCODE"] != DBNull.Value)
                {
                    cboSalesMan.SelectedValue = oRow["SMCODE"].ToString();
                }
                else
                {
                    cboAreaCode.SelectedIndex = -1;
                }
                if (oRow["CustomerGroup"] != DBNull.Value)
                {
                    cboGroup.SelectedValue = oRow["CustomerGroup"].ToString();
                }
                else
                {
                    cboGroup.SelectedIndex = -1;
                }
                if (oRow["Teritorry"] != DBNull.Value)
                {
                    cboTeritorrialCode.SelectedValue = oRow["Teritorry"].ToString();
                }
                else
                {
                    cboTeritorrialCode.SelectedIndex = -1;
                }


                if (oRow["AreaCode"] != DBNull.Value)
                {
                    cboAreaCode.SelectedValue = oRow["AreaCode"];
                }
                else
                {
                    cboAreaCode.SelectedIndex = -1;
                }


                lookUpBP.EditValue = oRow["BPCodeParent"].ToString();

                txtInstitution.Text = oRow["Institution"].ToString();
                txtStoreCode.Text = oRow["StoreCode"].ToString();
                cboDivision.Text = oRow["Division"].ToString();

                cboVATCode.Text = oRow["VATCode"].ToString();



                txtDisc1.Text = oRow["Disc1"].ToString();
                txtDisc2.Text = oRow["Disc2"].ToString();
                txtDisc3.Text = oRow["Disc3"].ToString();
                txtDisc4.Text = oRow["Disc4"].ToString();


                btnAdd.Text = "Update";
                txtCode.Enabled = false;
            }
        }
        #endregion

        bool IsCompleteData()
        {
            if (txtCode.Text.Trim() != "" && txtName.Text.Trim() != "" && txtTIN.Text.Trim() != "" && txtPaymentTerm.Text.Trim() != "" & txtCreditLimit.Text.Trim() != "" && txtPhone.Text.Trim() != "" && txtContactPerson.Text.Trim() != "")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        bool IsNumeric(string sValue)
        {
            try
            {
                int iValue = Convert.ToInt16(sValue);

                return true;
            }
            catch
            {
                return false;
            }
        }


        public void BindCardDivision()
        {
            CardDivisionManager oManager = new CardDivisionManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            cboDivision.ValueMember = "DivName";
            cboDivision.DisplayMember = "DivName";
            cboDivision.DataSource = oManager.GetDivision();
            cboDivision.Refresh();

            oManager.Close();
        }

        public void ShowSearch()
        {
            FND_BP oSearchBP = new FND_BP();

            oSearchBP.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            oSearchBP.ConnectionString = this.ConnectionString;
            oSearchBP.BusinessPartnerType = cboBPType.Text;
            oSearchBP.ShowDialog();

            if (oSearchBP.CardCode != null || oSearchBP.CardCode.Trim() != "")
            {
                txtCode.Text = oSearchBP.CardCode;
                txtName.Text = oSearchBP.CardName;
                //txtAddress.Text = oSearchBP.Address;
                txtStoreCode.Text = oSearchBP.StoreCode;

                txtUnitNo.Text = oSearchBP.UnitNo;
                txtStreetAddress.Text = oSearchBP.Street;
                txtBarangayOrDistrict.Text = oSearchBP.Baranggay;
                txtCityOrMunicipality.Text = oSearchBP.CityOrMunicipality;
                txtPostalCode.Text = oSearchBP.PostalCode;
                txtProvinceOrMetro.Text = oSearchBP.ProvinceOrMetro;

                txtContactPerson.Text = oSearchBP.ContactPerson;
                txtFax.Text = oSearchBP.Fax;
                txtPhone.Text = oSearchBP.Phone;
                txtTIN.Text = oSearchBP.TIN;
                txtPaymentTerm.Text = oSearchBP.PaymentTerm.ToString();
                txtCreditLimit.Text = oSearchBP.CreditLimit.ToString("#,###.00");
                txtWithHoldingTax.Text = oSearchBP.WithHoldingTax.ToString();

                if (oSearchBP.PriceList != null && oSearchBP.PriceList != "")
                {
                    cboPriceList.SelectedValue = oSearchBP.PriceList;
                }
                else
                {
                    cboPriceList.SelectedIndex = -1;
                }
                if (oSearchBP.BuyerSalesMan != null && oSearchBP.BuyerSalesMan != "")
                {
                    cboSalesMan.SelectedValue = oSearchBP.BuyerSalesMan;
                }
                else
                {
                    cboSalesMan.SelectedIndex = -1;
                }
                if (oSearchBP.CustomerGroup != null && oSearchBP.CustomerGroup != "")
                {
                    cboGroup.SelectedValue = oSearchBP.CustomerGroup;
                }
                else
                {
                    cboGroup.SelectedIndex = -1;
                }
                if (oSearchBP.Teritorry != null && oSearchBP.Teritorry != "")
                {
                    cboTeritorrialCode.SelectedValue = oSearchBP.Teritorry;
                }
                else
                {
                    cboTeritorrialCode.SelectedIndex = -1;
                }


                if (oSearchBP.AreaCode != null && oSearchBP.AreaCode != "")
                {
                    cboAreaCode.SelectedValue = oSearchBP.AreaCode;
                }
                else
                {
                    cboAreaCode.SelectedIndex = -1;
                }


                lookUpBP.EditValue = oSearchBP.ParentCode;

                txtInstitution.Text = oSearchBP.Institution;
                txtStoreCode.Text = oSearchBP.StoreCode;
                cboDivision.Text = oSearchBP.Division;

                cboVATCode.Text = oSearchBP.VATCode;
                cboDivision.Text = oSearchBP.Division;


                txtDisc1.Text = oSearchBP.Discount1;
                txtDisc2.Text = oSearchBP.Discount2;
                txtDisc3.Text = oSearchBP.Discount3;
                txtDisc4.Text = oSearchBP.Discount4;
                cboDocument.EditValue = oSearchBP.Document;

                oSearchBP.Dispose();

                btnAdd.Text = "Update";
                txtCode.Enabled = false;
            }
        }

        void BindCardDocument()
        {
            CardDocumentManager oManager = new CardDocumentManager();
            DataTable dtDocuments = new DataTable();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            dtDocuments = oManager.GetCardDocument();

            oManager.Close();

            cboDocument.Properties.Items.Clear();
            foreach (DataRow row in dtDocuments.Rows)
            {
                cboDocument.Properties.Items.Add(row["Code"]);
            }
        }
       
        private void BusinessPartner_Load(object sender, EventArgs e)
        {
            cboBPType.SelectedIndex = 1;
            cboDocument.EditValue = "N/A";
            BindPriceList();
            BindAreaCode();
            BindTeritorry();
            BindVAT();
            BindCardDivision();
            BindCardDocument();

            if (cboBPType.SelectedIndex == 0)
            {
                BindAccounts("Buyer");
                BindConsolidating("Vendor");
                BindPartnerGroup(false);
            }
            else
            {
                BindAccounts("Salesman");
                BindConsolidating("Customer");
                BindPartnerGroup(true);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {

                AddressBookUnit oCardUnit = new AddressBookUnit();
                AddressBookManager oBPManager = new AddressBookManager();

                oBPManager.ConnectionString = this.ConnectionString;
                oBPManager.Open();
                if (btnAdd.Text == "&Add")
                {
                    oCardUnit.Code = txtCode.Text;
                    if (oBPManager.IsRecordExists(oCardUnit.Code) == false)
                    {
                        if (IsCompleteData() == true)
                        {
                            if (IsNumeric(txtPaymentTerm.Text) == true)
                            {
                                oCardUnit.Name = txtName.Text;
                                oCardUnit.Type = cboBPType.Text;
                                oCardUnit.UnitNo = txtUnitNo.Text;
                                oCardUnit.Street = txtStreetAddress.Text;
                                oCardUnit.Barangay = txtBarangayOrDistrict.Text;
                                oCardUnit.CityOrMunicipality = txtCityOrMunicipality.Text;
                                oCardUnit.PostalCode = txtPostalCode.Text;
                                oCardUnit.ProvinceOrMetro = txtProvinceOrMetro.Text;
                                //oCardUnit.Address = txtAddress.Text;
                                oCardUnit.Phone = txtPhone.Text;
                                oCardUnit.Fax = txtFax.Text;
                                oCardUnit.ContactPerson = txtContactPerson.Text;
                                oCardUnit.TIN = txtTIN.Text;
                                oCardUnit.PaymentTerm = Convert.ToInt16(txtPaymentTerm.Text);
                                oCardUnit.CreditLimit = Convert.ToDouble(txtCreditLimit.Text);
                                oCardUnit.VATCode = cboVATCode.Text;

                                if (cboPriceList.SelectedIndex >= 0)
                                {
                                    oCardUnit.PriceList = cboPriceList.SelectedValue.ToString();
                                }
                                else
                                {
                                    oCardUnit.PriceList = "";
                                }
                                if (cboSalesMan.SelectedIndex >= 0)
                                {
                                    oCardUnit.BuyerSalesMan = cboSalesMan.SelectedValue.ToString();
                                }
                                else
                                {
                                    oCardUnit.BuyerSalesMan = "";
                                }
                                if (cboAreaCode.SelectedIndex >= 0)
                                {
                                    oCardUnit.AreaCode = cboAreaCode.SelectedValue.ToString();
                                }
                                else
                                {
                                    oCardUnit.AreaCode = "";
                                }

                                if (cboTeritorrialCode.SelectedIndex >= 0)
                                {
                                    oCardUnit.Teritorry = cboTeritorrialCode.SelectedValue.ToString();
                                }
                                else
                                {
                                    oCardUnit.Teritorry = "";
                                }

                                if (cboGroup.SelectedIndex >= 0)
                                {
                                    oCardUnit.CustomerGroup = cboGroup.SelectedValue.ToString();
                                }
                                else
                                {
                                    oCardUnit.CustomerGroup = "";
                                }

                                try
                                {
                                    if (lookUpBP.EditValue.ToString().Trim() != "" && lookUpBP.EditValue != null)
                                    {
                                        oCardUnit.ParentCode = lookUpBP.EditValue.ToString();
                                    }
                                    else
                                    {
                                        oCardUnit.ParentCode = "";
                                    }
                                }
                                catch
                                {
                                    oCardUnit.ParentCode = "";
                                }

                                oCardUnit.WithHoldingTax = Convert.ToDouble(txtWithHoldingTax.Text);
                                oCardUnit.Institution = txtInstitution.Text;
                                oCardUnit.StoreCode = txtStoreCode.Text;
                                oCardUnit.Division = cboDivision.Text;

                                oCardUnit.Discount1 = txtDisc1.Text;
                                oCardUnit.Discount2 = txtDisc2.Text;
                                oCardUnit.Discount3 = txtDisc3.Text;
                                oCardUnit.Discount4 = txtDisc4.Text;
                                oCardUnit.Document = cboDocument.Text;

                                oBPManager.AddPartnerCard(oCardUnit);

                                txtCode.Enabled = false;
                                ActionStatus.Text = "Operation Completed Succesfully.";
                                ActionStatus.BackColor = System.Drawing.Color.DarkBlue;
                                ActionStatusStrip.BackColor = System.Drawing.Color.DarkBlue;

                                timerInfo.Enabled = true;

                                oBPManager.Close();

                                btnAdd.Text = "&Update";
                            }
                            else
                            {
                                ActionStatus.Text = "Please insert numeric only to Payment Term.";
                                ActionStatus.BackColor = System.Drawing.Color.Red;
                                ActionStatusStrip.BackColor = System.Drawing.Color.Red;
                                timerInfo.Enabled = true;
                            }
                        }else
                        {
                            ActionStatus.Text = "Please fill all required information.";
                            ActionStatus.BackColor = System.Drawing.Color.Red;
                            ActionStatusStrip.BackColor = System.Drawing.Color.Red;
                            timerInfo.Enabled = true;
                        }
                    }
                    else
                    {
                        ActionStatus.Text = string.Format("{0} is already existing.", txtCode.Text.ToLowerInvariant());
                        ActionStatus.BackColor = System.Drawing.Color.DarkBlue;
                        ActionStatusStrip.BackColor = System.Drawing.Color.DarkBlue;
                        timerInfo.Enabled = true;
                    }
                }
                else
                {
                    oCardUnit.Code = txtCode.Text;
                    oCardUnit.Name = txtName.Text;
                    oCardUnit.Type = cboBPType.Text;
                    oCardUnit.UnitNo = txtUnitNo.Text;
                    oCardUnit.Street = txtStreetAddress.Text;
                    oCardUnit.Barangay = txtBarangayOrDistrict.Text;
                    oCardUnit.CityOrMunicipality = txtCityOrMunicipality.Text;
                    oCardUnit.PostalCode = txtPostalCode.Text;
                    oCardUnit.ProvinceOrMetro = txtProvinceOrMetro.Text;
                    oCardUnit.Phone = txtPhone.Text;
                    oCardUnit.Fax = txtFax.Text;
                    oCardUnit.ContactPerson = txtContactPerson.Text;
                    oCardUnit.TIN = txtTIN.Text;
                    oCardUnit.PaymentTerm = Convert.ToInt16(txtPaymentTerm.Text);
                    oCardUnit.CreditLimit = Convert.ToDouble(string.Format(txtCreditLimit.Text, "####.00"));
                    oCardUnit.VATCode = cboVATCode.Text;

                    if (cboPriceList.SelectedIndex >= 0)
                    {
                        oCardUnit.PriceList = cboPriceList.SelectedValue.ToString();
                    }
                    else
                    {
                        oCardUnit.PriceList = "";
                    }
                    if (cboSalesMan.SelectedIndex >= 0)
                    {
                        oCardUnit.BuyerSalesMan = cboSalesMan.SelectedValue.ToString();
                    }
                    else
                    {
                        oCardUnit.BuyerSalesMan = "";
                    }
                    if (cboAreaCode.SelectedIndex >= 0)
                    {
                        oCardUnit.AreaCode = cboAreaCode.SelectedValue.ToString();
                    }
                    else
                    {
                        oCardUnit.AreaCode = "";
                    }

                    if (cboTeritorrialCode.SelectedIndex >= 0)
                    {
                        oCardUnit.Teritorry = cboTeritorrialCode.SelectedValue.ToString();
                    }
                    else
                    {
                        oCardUnit.Teritorry = "";
                    }

                    if (cboGroup.SelectedIndex >= 0)
                    {
                        oCardUnit.CustomerGroup = cboGroup.SelectedValue.ToString();
                    }
                    else
                    {
                        oCardUnit.CustomerGroup = "";
                    }


                   
                    try
                    {
                        if (lookUpBP.EditValue.ToString().Trim() != "" && lookUpBP.EditValue != null)
                        {
                            oCardUnit.ParentCode = lookUpBP.EditValue.ToString();
                        }
                        else
                        {
                            oCardUnit.ParentCode = "";
                        }
                    }
                    catch
                    {
                        oCardUnit.ParentCode = "";
                    }

                    try
                    {
                         oCardUnit.Institution = txtInstitution.Text;
                    }
                    catch
                    {
                        oCardUnit.Institution = "";
                    }

                    try
                    {
                        oCardUnit.StoreCode = txtStoreCode.Text;
                    }
                    catch
                    {
                        oCardUnit.StoreCode = "";
                    }

                    oCardUnit.WithHoldingTax = Convert.ToDouble(txtWithHoldingTax.Text);
                    oCardUnit.Division = cboDivision.Text;
                    oCardUnit.StoreCode = txtStoreCode.Text;
                    oCardUnit.Discount1 = txtDisc1.Text;
                    oCardUnit.Discount2 = txtDisc2.Text;
                    oCardUnit.Discount3 = txtDisc3.Text;
                    oCardUnit.Discount4 = txtDisc4.Text;
                    oCardUnit.Document = cboDocument.Text;


                    oBPManager.UpdatePartner(oCardUnit);
                    
                    ActionStatus.Text = string.Format("{0} is already updated.", txtName.Text.TrimStart());
                    
                    ActionStatus.BackColor = System.Drawing.Color.DarkBlue;
                    ActionStatusStrip.BackColor = System.Drawing.Color.DarkBlue;
                    timerInfo.Enabled = true;
                    oBPManager.Close();
                    
                }
                oBPManager.Close();

                
            }
            catch(Exception ex)
            {
                ActionStatus.Text = ex.Message.ToString();

                ActionStatus.BackColor = System.Drawing.Color.DarkBlue;
                ActionStatusStrip.BackColor = System.Drawing.Color.DarkBlue;
                timerInfo.Enabled = true;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtCreditLimit_Leave(object sender, EventArgs e)
        {
            double _CreditLimit = 0;
            try
            {
                _CreditLimit = Convert.ToDouble(txtCreditLimit.Text); ;
                txtCreditLimit.Text = _CreditLimit.ToString("#,###.00");
            }
            catch
            {
                txtCreditLimit.Text = _CreditLimit.ToString();
            }
        }

        private void timerInfo_Tick(object sender, EventArgs e)
        {
            ActionStatus.Text = "";
            ActionStatus.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Control);
            ActionStatusStrip.BackColor = System.Drawing.Color.FromKnownColor(KnownColor.Control);

            timerInfo.Enabled = false;
        }

        private void cboBPType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPriceList();

            if (cboBPType.SelectedIndex == 0)
            {
                BindAccounts("Buyer");
                BindConsolidating("Vendor");
            }
            else
            {
                BindAccounts("Salesman");
                BindConsolidating("Customer");
            }
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void xtraScrollableControl1_Click(object sender, EventArgs e)
        {

        }

     
    }
}

